package com.sunwayworld.baseline.qywx.login;

import com.alibaba.fastjson.JSONObject;
import com.sunwayworld.baseline.qywx.login.bean.AppInfo;
import com.sunwayworld.baseline.qywx.login.bean.EnterpriseWxConf;
import com.sunwayworld.baseline.qywx.login.utils.RestUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SecurityFilter {
    private void error401(HttpServletResponse res) throws IOException {
        String fileJson = RestUtil.getJsonFromFile("enterprise_wx_conf");
        EnterpriseWxConf conf = JSONObject.parseObject(fileJson, EnterpriseWxConf.class);
        AppInfo appInfo = conf.getLimsAppInfo();
        RestResult r = RestResult.error(401, "session invalid");
        r.put("qywxLoginEnabled", conf.getQywxLoginEnabled());
        r.put("wwLoginUrl", conf.getWwLoginUrl());
        r.put("corpId", appInfo.getCorpId());
        r.put("agentId", appInfo.getAgentId());

        res.setStatus(HttpStatus.UNAUTHORIZED.value()); //401
        res.setContentType(MediaType.APPLICATION_JSON_VALUE);
        res.getWriter().write(JSONObject.toJSONString(r));
        res.flushBuffer();
    }
}
